package ru.nsu.fit.client.core.soap.send_question;

import org.ksoap2.serialization.SoapObject;
import ru.nsu.fit.client.core.soap.SOAPExecutor;

import java.util.AbstractMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

public class SendQuestion {
    private static final String WSDL_NAME = "send_question";
    private static final String METHOD_NAME = "askQuestion";
    private static final String NAMESPACE = "http://send_question.ws.fit.nsu.ru/";
	
	public static SendQuestionOutput execute(final String serverLocation, long userSessionId, final String text, final List<String> cases) throws Exception
	{
		String wsdlLocation = serverLocation + WSDL_NAME + "?wsdl";
		
		Set<Entry<String, String>> params = new HashSet<Entry<String,String>>();
		params.add(new AbstractMap.SimpleEntry<String, String>("userSessionId", String.valueOf(userSessionId)));
		params.add(new AbstractMap.SimpleEntry<String, String>("questionText", text));
		for (String s : cases)
		{
			params.add(new AbstractMap.SimpleEntry<String, String>("questionOptions", s));
		}
		
		SoapObject response = SOAPExecutor.executeSOAP(wsdlLocation, NAMESPACE, METHOD_NAME, params);
			
		SendQuestionOutput out = new SendQuestionOutput();
		out.setErrorCode(Integer.parseInt(response.getPropertyAsString("errorCode")));
		if (response.hasProperty("errorMessage"))
		{
			out.setErrorMessage(response.getPropertyAsString("errorMessage"));
		}
		if (response.hasProperty("questionId"))
		{
			out.setQuestionId(Integer.parseInt(response.getPropertyAsString("questionId")));
		}
		
		return out;
	}
}
